package com.seadragon.app.ksr.util;

import com.seadragon.app.ksr.model.LatLng;

public class GeoUtils {
	public static boolean insidePolygon(LatLng pt, int start, int end, LatLng[] boundary) {
		boolean inside = false;
		for(int i = start, j= end-1; i < end; j=i++){
			if((boundary[i].getLat() > pt.getLat()) != (boundary[j].getLat() > pt.getLat()) && pt.getLng() < (boundary[j].getLng()-boundary[i].getLng())*(pt.getLat()-boundary[i].getLat())/(boundary[j].getLat()-boundary[i].getLat())+boundary[i].getLng()){
				inside = !inside;
			}
		}
		return inside;
	}
	
	public static boolean hitPolygonUsingTriangle(LatLng pt, LatLng ct, LatLng[] boundary) {
		boolean hit = false;
		for (int i = 1; i < boundary.length; i++) {
			hit = hitTriangle(pt, ct, boundary[i - 1], boundary[i]);
			if (hit)
				return hit;
		}
		return hit;
	}

	public static boolean hitTriangle(LatLng pt, LatLng a, LatLng b, LatLng c) {
		LatLng v0 = c.subtract(a);
		LatLng v1 = b.subtract(a);
		LatLng v2 = pt.subtract(a);
		double dot00 = v0.dot(v0);
		double dot01 = v0.dot(v1);
		double dot02 = v0.dot(v2);
		double dot11 = v1.dot(v1);
		double dot12 = v1.dot(v2);
		double invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
		double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
		double v = (dot00 * dot12 - dot01 * dot02) * invDenom;

		// Check if point is in triangle
		return (u >= 0) && (v >= 0) && (u + v < 1);
	}

	public static boolean hitRectangle(LatLng pt, double minLat, double minLng, double maxLat, double maxLng) {
		return (pt.getLat() >= minLat && pt.getLat() <= maxLat && pt.getLng() >= minLng && pt.getLng() <= maxLng);
	}

//	public static boolean hit(double lat, double lng, County county) {
//		if (lat > county.getMaxLat() || lat < county.getMinLat())
//			return false;
//		if (lng > county.getMaxLng() || lng < county.getMinLng())
//			return false;
////		return hitPolygonUsingTriangle(new LatLng(lat, lng), new LatLng(county.getLat(), county.getLng()), BinUtils.convertToDoubleArray(county.getBoundary()));
//		String[] parts = county.getParts().split(",");
//		int[] offsets = new int[parts.length];
//		for(int i = 0; i < offsets.length; i++){
//			offsets[i] = Integer.parseInt(parts[i]);
//		}
//		LatLng[] boundary = BinUtils.convertToDoubleArray(county.getBoundary());
//		
//		boolean result = false;
//		for(int i = 0; i < county.getNumOfParts(); i++){
//			if(i ==  county.getNumOfParts()-1){
//				result |= insidePolygon(new LatLng(lat, lng), offsets[i], boundary.length, boundary);
//			}else{
//				result |= insidePolygon(new LatLng(lat, lng), offsets[i], offsets[i+1], boundary);
//			}
//		}
//		return result;
//	}
//
//	public static boolean hit(double lat, double lng, Place place) {
//		if (lat > place.getMaxLat() || lat < place.getMinLat())
//			return false;
//		if (lng > place.getMaxLng() || lng < place.getMinLng())
//			return false;
//		return hitPolygonUsingTriangle(new LatLng(lat, lng), new LatLng(place.getLat(), place.getLng()), BinUtils.convertToDoubleArray(place.getBoundary()));
//	}

}
